<template>
    <div>
        <!--          面包屑-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item >🏠</el-breadcrumb-item>
            <el-breadcrumb-item>饮食管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!--        卡片区域-->
        <el-card class="box-card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                            type="primary"
                            icon="el-icon-plus"
                            size="mini"
                            @click="handleAdd"
                            v-hasPermi="['system:sugar:add']"
                    >新增</el-button>
                </el-col>
            </el-row>
            <!--            文章数据展示区域-->
            <el-table v-loading="loading" :data="adminArticleList">
                <el-table-column
                        type="index"
                        label="#"
                        width="100">
                </el-table-column>

                <el-table-column label="饮食内容" align="center" width="700px" prop="introduce">
                    <template slot-scope="{ row }">
                        <el-button :plain="true" @click="openHTML(row.introduce)" style="height: 80px" type="text" >
                            <div
                                    id="introduce"
                                    v-html="row.introduce"
                                    style="
                                         overflow: hidden;
                                         text-overflow: ellipsis;
                                         white-space: nowrap;
                                         width: 100%;
                                         height: 100px;
                                         text-align: center;"
                            ></div>
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="text"
                                icon="el-icon-edit"
                                @click="handleUpdate(scope.row)"
                        >修改</el-button>
                        <el-button
                                size="mini"
                                type="text"
                                icon="el-icon-delete"
                                @click="shangchu(scope.row.id)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--     分页     -->
            <el-pagination
                    v-show="total>0"
                    :page-size="queryParams.pageSize"
                    :pager-count="11"
                    :current-page.sync="queryParams.pageNum"
                    :total="total"
                    @current-change="getList"
            >
            </el-pagination>
            <!-- 添加或修改article对话框 -->
            <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
                <el-form ref="form" :model="form" :rules="rules" label-width="80px">

                    <el-form-item label="饮食内容">
                        <editor v-model="form.introduce" :min-height="300"/>
                    </el-form-item>

                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>

</template>

<script>
    // import marked from "marked";
    // import hljs from "highlight.js";
    import "highlight.js/styles/monokai-sublime.css";

    import Editor from '@/components/Editor';


    export default {
        name: "wenzhang",
        components: { Editor },
        data(){
            return{
                adminArticleList:[],
                neiirong:'',
                biaoti:'',
                zuozhe:'',
                value:'',
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                },
                // 弹出对话框标题
                title: "",
                // 修改对话框开关控制
                open :false,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    article:{

                        introduce: null,

                    },
                    sidx: 'id',
                    sord: 'DESC',

                },
            }
        },
        created() {
            this.getList()
        },


        methods:{
            async getList() {
                this.loading = true;
                const token = sessionStorage.getItem("token");
                this.$http.defaults.headers.common['Authorization'] = token;
                const {data: response} = await this.$http.get("/food/list?pageNum="+this.queryParams.pageNum);
                this.adminArticleList = response.data.data;
                this.total = response.data.total;
                this.loading = false;
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                    this.form = row;
                    this.title = "修改饮食信息";
                    this.open = true;
            },
            resetQuery() {
                this.queryParams={
                    pageNum: 1,
                    pageSize: 10,
                    article:{
                        introduce: null,
                    },
                    sidx: 'id',
                    sord: 'DESC',

                },
                    // 排序选择
                    this.option=null,
                    this.issueTime=null,
                    this.handleQuery();
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            // this.form.time = this.form.date1 + " "+this.form.date2;
                            console.log(this.form);
                            console.log(this.form.time);
                            const token = sessionStorage.getItem("token");
                            this.$http.defaults.headers.common['Authorization'] = token;
                            console.log(this.form)
                            this.$http.put('food', this.form).then(response => {
                                if (response.data.code === 200) {
                                    this.$message({showClose: true, message: "修改成功", type: "success"})
                                    this.open = false;
                                    this.getList()
                                }
                            });
                        }else{
                            console.log(this.form);
                            console.log(this.form.time);
                            const token = sessionStorage.getItem("token");
                            this.$http.defaults.headers.common['Authorization'] = token;
                            console.log(this.form)
                            this.$http.post('food', this.form).then(response => {
                                if (response.data.code === 200) {
                                    this.$message({showClose: true, message: "添加成功", type: "success"})
                                    this.open = false;
                                    this.getList()
                                }
                            });
                        }
                    }
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            handleAdd(){
                this.open = true;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    introduce: null,
                };
                // this.resetForm("form");
            },
            // 打开文章详情
            openHTML(introduce) {
                this.$message({
                    dangerouslyUseHTMLString: true,
                    message: introduce,
                    duration: 1000,
                });
            },
            async getadminArticleList() {
                const token = sessionStorage.getItem("token");
                this.$http.defaults.headers.common['Authorization'] = token;
                const {data: res} = await this.$http.get('food/list')
                console.log(res.data)
                this.adminArticleList = res.data.data
            },
            async shangchu(id){
                const ids = id ;
                this.$confirm('是否确认删除?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    const token = sessionStorage.getItem("token");
                    this.$http.defaults.headers.common['Authorization'] = token;
                    this.$http.delete(`food/${ids}`)
                    this.getList();
                    this.$message("删除成功");
                });
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },


        },

    }
</script>

<style scoped>
    .el-breadcrumb{
        margin-bottom: 12px;

    }
    .el-card{
        box-shadow: 0 1px 1px rgba(0,0,0,0.15);
    }
    .chaxun{

        height: 40px;
        text-indent: 1em;
        line-height: 2.5em;
        vertical-align: middle;
    }

</style>
